#include "x_sdk.h"

char *HttpCommunicate(char *body,char *uri)
{
	char *resrvdata = NULL;
	char *ret_data = NULL;
	ghttp_request *conntoserv = ghttp_request_new();
	ghttp_set_uri(conntoserv,uri);
	ghttp_set_type(conntoserv, ghttp_type_post); 		//在set body前必须这样
	ghttp_set_body(conntoserv,body,strlen(body));
	ghttp_prepare(conntoserv);
	if(ghttp_process(conntoserv) == ghttp_error)
	{
		return NULL;
	}
	int p = ghttp_status_code(conntoserv);
	switch(p)
	{
	case 404:	return NULL;
	case 200:	resrvdata = ghttp_get_body(conntoserv);
				ret_data = new char[strlen(resrvdata) + 1];
				strcpy(ret_data, resrvdata);
				break;
	default:
		break;
	}

	ghttp_close(conntoserv);
	ghttp_request_destroy(conntoserv);

	return ret_data;
}

static int get_int(json_object *p_object, const char *p_key)
{
	json_object *pObject = NULL;
	pObject = json_object_object_get(p_object,(char *)p_key);
	if (!is_error(pObject))
		return json_object_get_int(pObject);

	J_OS::LOGINFO("json get_int error, key = %s", p_key);
	return J_JSON_UNKOWN;
}

static char *get_string(json_object *p_object, const char *p_key)
{
	json_object *pObject = NULL;
	pObject = json_object_object_get(p_object,(char *)p_key);
	if (!is_error(pObject))
		return json_object_get_string(pObject);

	J_OS::LOGINFO("json get_string error, key = %s", p_key);
	return NULL;
}

static json_object *get_object(json_object *p_object, const char *p_key)
{
	json_object *pObject = NULL;
	pObject = json_object_object_get(p_object,(char *)p_key);
	if (!is_error(pObject))
		return pObject;

	J_OS::LOGINFO("json get_string error, key = %s", p_key);
	return NULL;
}

r_register *StreamServerResgister(int ssid,char *uri)
{
	char *buf,*body;
	r_register *r_data = NULL;
	json_object *helper = NULL;
	buf = body = NULL;

	/*生成发送数据*/
	json_object *jsojt = json_object_new_object();
	json_object_object_add(jsojt,(char *)"cmd",json_object_new_int(1));
	json_object *helpjs = json_object_new_object();
	json_object_object_add(helpjs,(char *)"id",json_object_new_int(ssid));
	json_object_object_add(jsojt,(char *)"parm",helpjs);
	body = new char[strlen(json_object_to_json_string(jsojt))+1];
	strcpy(body,json_object_to_json_string(jsojt));

	/*发送并接受数据*/
	buf = HttpCommunicate(body,uri);
	delete body;
	if(buf == NULL)
	{
		return r_data;
	}

	/*解析数据*/
	helper = json_object_new_object();
	r_data = new r_register;

	helper = json_tokener_parse(buf);
	if(is_error(helper))
	{
		J_OS::LOGINFO("StreamServerResgister json_tokener_parse error");
		J_OS::LOGINFO("json_data = %s", buf);
		return NULL;
	}
	r_data->cmd = get_int(helper, "cmd");
	r_data->rst = get_int(helper, "rst");

	helper = get_object(helper, (char *)"parm");
	if(!helper)
	{
		J_OS::LOGINFO("StreamServerResgister json_object_object_get error");
		return NULL;
	}
	r_data->parm.store		= get_string(helper, "strore");
	r_data->parm.segment_h 	= get_int(helper, "segment_h");
	r_data->parm.segment_a 	= get_int(helper, "segment_a");

	helper = get_object(helper, (char *)"res");
	if(!helper)
	{
		J_OS::LOGINFO("StreamServerResgister json_object_object_get error");
		return NULL;
	}
	json_object *otemp = json_object_new_object();
	json_object *cha   = json_object_new_object();
	for(int i=0;i<json_object_array_length(helper);i++)
	{
		otemp = json_object_array_get_idx(helper,i);
		_res *rtemp = new _res;
		if(rtemp ==NULL) {return NULL;}
		rtemp->id		= get_int(otemp, "id");
		rtemp->type 	= get_int(otemp, "type");
		rtemp->ip	 	= get_string(otemp, "ip");
		rtemp->port 	= get_int(otemp, "port");
		rtemp->user 	= get_string(otemp, "user");
		rtemp->pass 	= get_string(otemp, "pass");
		otemp = json_object_object_get(otemp,(char *)"cha");
		r_data->parm.resnum.push_back(*rtemp);
		for(int j=0;j<json_object_array_length(otemp);j++)
		{
			cha = json_object_array_get_idx(otemp,j);
			_cha *ctemp = new _cha;
			if(ctemp ==NULL)
			{
				return NULL;
			}
			ctemp->cha		= get_int(cha, "cha");
			ctemp->resid	= get_string(cha, "resid");
			ctemp->ms		= get_int(cha, "ms");
			r_data->parm.resnum[i].chanum.push_back(*ctemp);
			delete ctemp;
			ctemp = NULL;
		}
		delete rtemp;
		rtemp = NULL;
	}
	delete buf;
	return r_data;
}

r_devconfig *GetDevConfigByResid(char *resid,char *uri)
{
	char *buf,*body;
	r_devconfig *r_data = NULL;
	json_object *helper = NULL;

	/*生成发送数据*/
	json_object *jsojt = json_object_new_object();
	json_object_object_add(jsojt,(char *)"cmd",json_object_new_int(2));
	json_object *helpjs = json_object_new_object();
	json_object_object_add(helpjs,(char *)"id",json_object_new_string(resid));
	json_object_object_add(jsojt,(char *)"parm",helpjs);
	body = new char[strlen(json_object_to_json_string(jsojt))+1];
	strcpy(body,json_object_to_json_string(jsojt));

	/*发送并接受数据*/
	buf = HttpCommunicate(body,uri);
	delete body;
	if(buf == NULL) {return r_data;}

	/*解析数据*/
	helper = json_object_new_object();
	r_data = new r_devconfig;

	helper = json_tokener_parse(buf);
	if(is_error(helper))
	{
		J_OS::LOGINFO("GetDevConfigByResid json_tokener_parse error");
		J_OS::LOGINFO("json_data = %s", buf);
		return NULL;
	}
	r_data->rst = get_int(helper, "rst");

	helper = get_object(helper,(char *)"parm");
	if(!helper)
	{
		J_OS::LOGINFO("GetDevConfigByResid json_object_object_get error");
		return NULL;
	}
	r_data->parm.id		= get_int(helper, "id");
	r_data->parm.type		= get_string(helper, "type");
	r_data->parm.ip		= get_string(helper, "ip");
	r_data->parm.port		= get_int(helper, "port");
	r_data->parm.user		= get_string(helper, "user");
	r_data->parm.pass		= get_string(helper, "pass");
	r_data->parm.cha		= get_int(helper, "cha");
	r_data->parm.resid	= get_string(helper, "resid");
	r_data->parm.ms		= get_int(helper, "ms");
	r_data->parm.store	= get_int(helper, "store");

	delete buf;
	return r_data;
}

int GetRealTimePermission(char *resid,char *uid,char *uri)
{
	char *buf,*body;
	int r_data = -1;
	json_object *helper = NULL;

	/*生成发送数据*/
	json_object *jsojt = json_object_new_object();
	json_object_object_add(jsojt,(char *)"cmd",json_object_new_int(3));
	json_object *helpjs = json_object_new_object();
	json_object_object_add(helpjs,(char *)"resid",json_object_new_string(resid));
	json_object_object_add(helpjs,(char *)"uid",json_object_new_string(uid));
	json_object_object_add(jsojt,(char *)"parm",helpjs);
	body = new char[strlen(json_object_to_json_string(jsojt))+1];
	strcpy(body,json_object_to_json_string(jsojt));

	/*发送并接受数据*/
	buf = HttpCommunicate(body,uri);
	delete body;
	if(buf == NULL) {return r_data;}

	/*解析数据*/
	helper = json_object_new_object();
	helper = json_tokener_parse(buf);
	if(is_error(helper))
	{
		J_OS::LOGINFO("GetRealTimePermission json_tokener_parse error");
		J_OS::LOGINFO("json_data = %s", buf);
		return NULL;
	}
	r_data = get_int(helper, "rst");

	delete buf;
	return r_data;
}

r_historyfile *GetHistoryFile(char *resid,char *uid,time_t stime,time_t etime,char *uri)
{
	char *buf,*body;
	r_historyfile *r_data = NULL;
	json_object *helper = NULL;

	/*生成发送数据*/
	json_object *jsojt = json_object_new_object();
	json_object_object_add(jsojt,(char *)"cmd",json_object_new_int(4));
	json_object *helpjs = json_object_new_object();
	json_object_object_add(helpjs,(char *)"resid",json_object_new_string(resid));
	json_object_object_add(helpjs,(char *)"uid",json_object_new_string(uid));
	json_object_object_add(helpjs,(char *)"stime",json_object_new_double(stime));
	json_object_object_add(helpjs,(char *)"etime",json_object_new_double(etime));
	json_object_object_add(jsojt,(char *)"parm",helpjs);
	body = new char[strlen(json_object_to_json_string(jsojt))+1];
	strcpy(body,json_object_to_json_string(jsojt));

	/*发送并接受数据*/
	buf = HttpCommunicate(body,uri);
	delete body;
	if(buf == NULL) {return r_data;}

	/*解析数据*/
	helper = json_object_new_object();
	r_data = new r_historyfile;
	helper = json_tokener_parse(buf);
	if(is_error(helper))
	{
		J_OS::LOGINFO("GetHistoryFile json_tokener_parse error");
		J_OS::LOGINFO("json_data = %s", buf);
		return NULL;
	}
	r_data->rst = get_int(helper,  "rst");

	helper = get_object(helper, (char *)"parm");
	if(!helper)
	{
		J_OS::LOGINFO("GetHistoryFile json_object_object_get error");
		return NULL;
	}
	r_data->parm.dvr		= get_int(helper,  "dvr");

	helper	= get_object(helper, (char *)"files");
	if(!helper)
	{
		J_OS::LOGINFO("GetHistoryFile json_object_object_get error");
		return NULL;
	}

	int arrlen = json_object_array_length(helper);
	string tmp;
	for(int i=0;i<arrlen;i++)
	{
		tmp = json_object_get_string(json_object_array_get_idx(helper, i));
		r_data->parm.files.push_back(tmp);
	}
	delete buf;
	return r_data;
}

int GetWarningMsg(char *resid,int type,char *uri)
{
	char *buf,*body;
	int r_data = -1;
	json_object *helper = NULL;

	/*生成发送数据*/
	json_object *jsojt = json_object_new_object();
	json_object_object_add(jsojt,(char *)"cmd",json_object_new_int(5));
	json_object *helpjs = json_object_new_object();
	json_object_object_add(helpjs,(char *)"resid",json_object_new_string(resid));
	json_object_object_add(helpjs,(char *)"type",json_object_new_int(type));
	json_object_object_add(jsojt,(char *)"parm",helpjs);
	body = new char[strlen(json_object_to_json_string(jsojt))+1];
	strcpy(body,json_object_to_json_string(jsojt));

	/*发送并接受数据*/
	buf = HttpCommunicate(body,uri);
	delete body;
	if(buf == NULL) {return r_data;}

	/*解析数据*/
	helper = json_object_new_object();
	helper = json_tokener_parse(buf);
	if(is_error(helper))
	{
		J_OS::LOGINFO("GetWarningMsg json_tokener_parse error");
		J_OS::LOGINFO("json_data = %s", buf);
		return NULL;
	}
	r_data = get_int(helper, "rst");

	delete buf;
	return r_data;
}

int GetRecordNotice(s_record &record,char *uri)
{
	char *buf,*body;
	buf = body =NULL;
	int r_data = -1;
	json_object *helper = NULL;

	/*生成发送数据*/
	json_object *jsojt = json_object_new_object();
	json_object *helpjs = json_object_new_object();
	json_object *jsonarr = json_object_new_array();
	json_object_object_add(jsojt,(char *)"cmd",json_object_new_int(6));
	json_object_object_add(helpjs,(char *)"resid",json_object_new_string((char *)record.resid.c_str()));
	json_object_object_add(helpjs,(char *)"files",jsonarr);
	for(vector<s_fileinfo>::iterator iter = record.filenum.begin();
			iter != record.filenum.end();
			iter++)
	{
		json_object *jotemp = json_object_new_object();
		json_object_object_add(jotemp,(char *)"file",json_object_new_string((char *)iter->file.c_str()));
		json_object_object_add(jotemp,(char *)"stime",json_object_new_double(iter->stime));
		json_object_object_add(jotemp,(char *)"etime",json_object_new_double(iter->etime));
		json_object_array_add(jsonarr,jotemp);
	}
	json_object_object_add(jsojt,(char *)"parm",helpjs);
	body = new char[strlen(json_object_to_json_string(jsojt))+1];
	strcpy(body,json_object_to_json_string(jsojt));

	/*发送并接受数据*/
	buf = HttpCommunicate(body,uri);

	delete body;
	body = NULL;
	if(buf == NULL) {return r_data;}

	/*解析数据*/
	helper = json_object_new_object();
	helper = json_tokener_parse(buf);
	if(is_error(helper))
	{
		J_OS::LOGINFO("GetWarningMsg json_tokener_parse error");
		J_OS::LOGINFO("json_data = %s", buf);
		return NULL;
	}
	r_data = get_int(helper, "rst");

	delete buf;
	return r_data;
}








